function [A,SIGMA,U,V] = olsvarc(y,p)
% This program estimates a level VAR with intercept in companion form by
% OLS.


% t denotes the number of observation on each variable, where there are q
% total variables:
[t,q]           =   size(y); 

y               =   y';
Y               =   y(:,p:t);
for ix = 1:p-1
    % This forms the companion matrix:
    Y               =   [Y; y(:,p-ix:t-ix)]; 
end;

X               =   [ones(1,t-p); Y(:,1:t-p)];
Y               =   Y(:,2:t-p+1);
A               =   (Y*X')/(X*X');
%The first two rows of U will be the reduced form residuals:
U               =   Y-A*X; 
% The upper left hand block gives the variance-covariance matrix:
SIGMA           =   U*U'/(t-p-1); 
%This gives the intercept terms:
V               =   A(:,1); 
% This gives the slope coefficients:
A               =   A(:,2:q*p+1);  
